package com.seeyon.apps.kkSample.utils.cap4;

import com.seeyon.apps.kkSample.utils.jdbc.JDBCAgentUtil;
import com.seeyon.cap4.form.bean.*;
import com.seeyon.cap4.form.service.CAP4FormManager;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.filemanager.manager.AttachmentManager;
import com.seeyon.ctp.common.filemanager.manager.FileManager;
import com.seeyon.ctp.common.log.CtpLogFactory;
import org.apache.commons.logging.Log;

/**
 * @Author: L
 * @Description:
 * @Date: 创建于10:18 2022-10-24
 **/
public class Cap4FormUtil {

    private static final Log LOG = CtpLogFactory.getLog(JDBCAgentUtil.class);
    private static FileManager fileManager = (FileManager) AppContext.getBean("fileManager");
    private static AttachmentManager attachmentManager = (AttachmentManager) AppContext.getBean("attachmentManager");
    private static CAP4FormManager cap4FormManager = (CAP4FormManager) AppContext.getBean("cap4FormManager");

    /**
     * 获取主表字段在数据库中的值
     *
     * @param name               控件名称或者field000*
     * @param formDataMasterBean
     * @return
     */
    public static Object getFieldValue(String name, FormDataMasterBean formDataMasterBean) {
        try {
            FormTableBean tableBean = formDataMasterBean.getFormTable();
            FormFieldBean field = tableBean.getFieldBeanByDisplay(name);
            if (null == field) {
                field = tableBean.getFieldBeanByName(name);
            }
            return formDataMasterBean.getFieldValue(field.getName());
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取主表字段在表单中的显示值
     * @param name	控件名称或者field000*
     * @param formDataBean
     * @return
     */
    public static String getFieldDisplay(FormDataMasterBean formDataBean,String name) {
        try {
            FormTableBean tableBean = formDataBean.getFormTable();
            FormFieldBean field = tableBean.getFieldBeanByDisplay(name);
            if(null == field) {
                field = tableBean.getFieldBeanByName(name);
            }
            Object[]  obj = field.getDisplayValue(formDataBean.getFieldValue(field.getName()), true);
            return obj[1].toString();
        } catch (Exception e) {
            return null;
        }
    }


    /**
     * 获取明细表字段在数据库中的值
     *
     * @param name         控件名称或者field000*
     * @param formDataBean
     * @return
     */
    public static Object getSubFieldDisplay(String name, FormDataSubBean formDataBean) {
        try {
            FormTableBean tableBean = formDataBean.getFormTable();
            FormFieldBean field = tableBean.getFieldBeanByDisplay(name);
            if (null == field) {
                field = tableBean.getFieldBeanByName(name);
            }
            return formDataBean.getFieldValue(field.getName());
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取从表字段在表单中的显示值
     *
     * @param name         控件名称或者field000*
     * @param formDataBean
     * @return
     */
    public static String getSubFieldValue(String name, FormDataSubBean formDataBean) {
        try {
            FormTableBean tableBean = formDataBean.getFormTable();
            FormFieldBean field = tableBean.getFieldBeanByDisplay(name);
            if (null == field) {
                field = tableBean.getFieldBeanByName(name);
            }
            Object[] obj = field.getDisplayValue(formDataBean.getFieldValue(field.getName()), true);
            return obj[1].toString();
        } catch (Exception e) {
            return null;
        }
    }



    /**
     * 通过控件名称获取field00**
     *
     * @param fieldDisplay 控件名称
     * @param tableBean
     * @return
     */
    public static String getFieldName(String fieldDisplay, FormTableBean tableBean) {
        FormFieldBean field = tableBean.getFieldBeanByDisplay(fieldDisplay);
        if (field != null) {
            return field.getName();
        }
        return null;
    }
    /**
     * 通过控件名称获取field00**
     *
     * @param fieldDisplay 控件名称
     * @param tableBean
     * @return
     */
    public static FormFieldBean getFormFieldBean(String fieldDisplay, FormTableBean tableBean) {
        FormFieldBean field = tableBean.getFieldBeanByDisplay(fieldDisplay);
        if (field != null) {
            return field;
        }
        return null;
    }
    /**
     * 通过控件名称获取field00**
     *
     * @param fieldDisplay 控件名称
     * @param formBean
     * @return
     */
    public static String getFieldName(String fieldDisplay, FormBean formBean) {
        FormFieldBean field = formBean.getFieldBeanByDisplay(fieldDisplay);
        if (field != null) {
            return field.getName();
        }
        return null;
    }
}
